package system;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ciummo
 */




public class Utilities implements Serializable{
    
    
    private final String FAIL = "fail";

    public Object callRemoteBean(String name) throws NamingException {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
//        props.setProperty("java.naming.provider.url", "jnp://10.114.211.130:1099");
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        InitialContext ctx = new InitialContext(props);
        return ctx.lookup(name);
    }
    
    public String checkRole(HttpServletRequest request)
            throws Exception {
        
        

        HttpSession httpSession = request.getSession();
        
        if (httpSession==null){
            
            return FAIL;
        }
        
        LoginActionForm loginForm = (LoginActionForm) httpSession.getAttribute("LoginActionForm");
        
        String role = null;
        
        try{
            role = loginForm.getRole();
        }catch(NullPointerException e){
            return FAIL;
        }
        
        if (role==null){
            
            return FAIL;
        }
        
                
        return loginForm.getRole();
    }
    
    
    
    
//ritorna la data di oggi

    public String today() {
        Calendar cal = new GregorianCalendar();
        DateFormat df = DateFormat.getDateInstance();
        return df.format(cal.getTime());

    }
    //ritorna la data di

    public String todayAddTime(int field, int n) {
        Calendar cal = new GregorianCalendar();
        DateFormat df = DateFormat.getDateInstance();
        cal.add(field, n);
        return df.format(cal.getTime());

    }

    public Date convertFromString(String date) {
        try {

            DateFormat df = DateFormat.getDateInstance();
            return df.parse(date);
        } catch (ParseException ex) {
            return null;
        }
    }

    public String convertToString(Date date) {
        try {
            DateFormat df = DateFormat.getDateInstance();
            return df.format(date);
        } catch (Exception ex) {
            return null;
        }
    }

    public Date convertTimeFromString(String time) {
        try {

            DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
            Date date = new Date();
            String data=df.format(date);
            return df.parse(time);
        } catch (ParseException ex) {
            return null;
        }
    }
}